package logic

import (
	"context"
	"xialou_platform/app/requestx"
	"xialou_platform/app/respx"
	"xialou_platform/app/tools"
	"xialou_platform/services/wallet/rpc/walletClient"

	"xialou_platform/services/wallet/api/internal/svc"
	"xialou_platform/services/wallet/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type IncomeDetailsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewIncomeDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) IncomeDetailsLogic {
	return IncomeDetailsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *IncomeDetailsLogic) IncomeDetails(req *types.IncomeDetailsReq) (resp *respx.PageResp, err error) {

	agentId, err := requestx.FetchUserIdByJwt(l.ctx)
	if err != nil {
		return nil, err
	}

	if len(req.BusinessId) > 0 {
		agentId = respx.IDParse(req.BusinessId).GetInt64()
	}

	details, err := l.svcCtx.WalletRpc.WalletDetails(l.ctx, &walletClient.WalletDetailsReq{
		Page:     req.Page,
		PageSize: req.PageSize,
		SourceId: agentId,
		Type:     req.Type,
	})
	if err != nil {
		return nil, err
	}

	list := make([]*types.IncomeDetailsResp, 0)
	for _, detail := range details.List {
		list = append(list, &types.IncomeDetailsResp{
			Icon:        detail.Icon,
			Description: detail.Description,
			Type:        detail.Type,
			Amount:      detail.Amount,
			Balance:     detail.Balance,
			CreateTime:  tools.TimeUnixShowLayoutString(detail.CreateTime),
		})
	}
	resp = respx.NewPageResp(req.Page, req.PageSize, details.GetTotal(), list)
	return
}
